Hi
ANy better/condensed way ?
while (var1=='y' || var1=='Y')
Hi
ANy better/condensed way ?
while (var1=='y' || var1=='Y')
maybe
while(toupper(var1) == 'Y')
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I don't think the second solution is better, as the function
call is more expensive than a byte comparison.
The 'better way' could be interpreted as 'more efficient', despite _indeed_ I don't see
what could be really improved here... nevermind.
it was asked about condenced way...
if talked about optimizing we can use
form and profile it...Code:while (var1=='y' | var1=='Y')
but supposing the input is get from user - what the sence to optimize for speed?
so the only reason to modify the code - make it more readable...
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
>> That was sarcasm.
Heh. Probably wasn't need to qualify that.
I think you're just going to have to live with it. If you want to feel better about it you could wrap it in a preprocessor or inline function. The thing is that it only looks nasty and messy but it's probably the best you can do. toupper is also not a bad alternative if you want something that looks more elegant.
You could just ask the user to use lower or upper letters.
If you insist, you can write it like this:It relies on the fact that upper and lower case differ by one bit in ASCII though.Code:const char lowerDiff = 'a' - 'A'; while ((var1 | lowerDiff) == 'y')
But if you worry about efficiency at this level you'll never get anything done.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Depends on the context. The C language itself is case-sensitive. This is something that users of the language simply have to deal with.
Although I agree in principle that a simple yes/no choice should probably be case insensitive simply for convenience, there could certainly be legitimate reasons to require input to be a certain form.